package com.ruoyi.timeoutttt;

import com.ruoyi.houseTask.domain.THouseNucleicTask;
import com.ruoyi.houseTask.service.ITHouseNucleicTaskService;
import com.ruoyi.timeout.domain.TNucleicTimeout;
import com.ruoyi.timeout.service.ITNucleicTimeoutService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@Configuration      //1.主要用于标记配置类，兼备Component的效果。
@EnableScheduling   // 2.开启定时任务
public class ScheduleTask {

    @Autowired
    private ITHouseNucleicTaskService houseNucleicTaskService;
    @Autowired
    private ITNucleicTimeoutService nucleicTimeoutService;
    //3.添加定时任务
   @Scheduled(cron = "0/5000000 * * * * ?")
    //或直接指定时间间隔，例如：5秒
    //@Scheduled(fixedRate=5000)
   private void configureTasks() {
        System.err.println("执行静态定时任务时间: " + LocalDateTime.now());

        THouseNucleicTask tHouseNucleicTask = new THouseNucleicTask();

        List<THouseNucleicTask> tHouseNucleicTasks = houseNucleicTaskService.selectTHouseNucleicTaskList(tHouseNucleicTask);

        TNucleicTimeout tNucleicTimeout = new TNucleicTimeout();

        for (THouseNucleicTask houseNucleicTask : tHouseNucleicTasks) {
            //住户信息
            //   System.out.println(houseNucleicTask);

            String status = houseNucleicTask.getStatus();
            int i = Integer.parseInt(status);

            if (i == 0 || i == 3 ){ //状态== 0 ，则报警
                System.out.println("超时了");
            }

            Date now = new Date();
            Date endTime = houseNucleicTask.getEndTime();
            boolean boo = now.getTime() - endTime.getTime() >0;


            if (i == 0 && boo == true){
                //住户核酸任务，超时表
                BeanUtils.copyProperties(houseNucleicTask,tNucleicTimeout);

                nucleicTimeoutService.insertTNucleicTimeout(tNucleicTimeout);

                houseNucleicTask.setStatus("3");
                houseNucleicTaskService.updateTHouseNucleicTask(houseNucleicTask);

            }

        }




    }
}
/*
*
        for (THouseNucleicTask houseNucleicTask : tHouseNucleicTasks) {
            //住户信息
         //   System.out.println(houseNucleicTask);
            String status = houseNucleicTask.getStatus();
            int i = Integer.parseInt(status);

            if (i == 0){
                System.out.println("超时了");

                BeanUtils.copyProperties(houseNucleicTask,tNucleicTimeout);

                nucleicTimeoutService.insertTNucleicTimeout(tNucleicTimeout);
            }

        }
*/
